import axios from "axios"
const user = {
  namespaced: true,
  state () {
    return {
      goodsList: []
    }

  },
  mutations: {
    setGoodsList (state, value) {
      state.goodsList = value
    }
  },
  actions: {
    async asyncGetGoodsList (store) {
      const url = `https://www.escook.cn/api/cart`
      // 发送异步请求
      const res = await axios({ url: url })
      store.commit('setGoodsList', res.data.list) // 提交mutation修改state中的数据
    }
  },
  getters: {
    // 结算数量
    allCount (state) {
      return state.goodsList.filter((item) => item.goods_state).reduce((a, b) => a += b.goods_count, 0)
    },
    allPrice (state) {
      return state.goodsList.filter(item => item.goods_state).reduce((a, b) => a += b.goods_count * b.goods_price, 0)
    }
  }
}
export default user